package vip.zhonghui.www.skills

import android.app.Activity
import android.app.ProgressDialog
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
import androidx.core.widget.addTextChangedListener
import com.google.gson.Gson
import org.json.JSONObject

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val username = findViewById<EditText>(R.id.username)
        val password = findViewById<EditText>(R.id.password)
        val login = findViewById<Button>(R.id.login)
        val register = findViewById<Button>(R.id.register)
        val save = findViewById<CheckBox>(R.id.save)

        val sp = getSharedPreferences("local", MODE_PRIVATE)
        val ed = sp.edit()

        username.setText(sp.getString("username",""))
        password.setText(sp.getString("password",""))
        save.isChecked=sp.getString("save","0")=="1"

        save.setOnCheckedChangeListener { buttonView, b ->
            if (save.isChecked){
                ed.putString("username",username.text.toString()).apply()
                ed.putString("password",password.text.toString()).apply()
                ed.putString("save","1").apply()
            }else{
                ed.putString("username","").apply()
                ed.putString("password","").apply()
                ed.putString("save","0").apply()
            }
        }

        username.addTextChangedListener {
            if (save.isChecked){
                ed.putString("username",username.text.toString()).apply()
                ed.putString("password",password.text.toString()).apply()
                ed.putString("save","1").apply()
            }else{
                ed.putString("username","").apply()
                ed.putString("password","").apply()
                ed.putString("save","0").apply()
            }
        }

        password.addTextChangedListener {
            if (save.isChecked){
                ed.putString("username",username.text.toString()).apply()
                ed.putString("password",password.text.toString()).apply()
                ed.putString("save","1").apply()
            }else{
                ed.putString("username","").apply()
                ed.putString("password","").apply()
                ed.putString("save","0").apply()
            }
        }


        login.setOnClickListener {
            val load = ProgressDialog.show(this,"","loading...",true,false)
            load.show()
            Http.post("login",JSONObject().apply {
                put("username",username.text.toString())
                put("password",password.text.toString())
            }){
                res->val data = Gson().fromJson(res,Data::class.java)
                runOnUiThread {
                    if (data.data.verifySuccess){
                        Toast.makeText(this, "${data.data.userInfo.username}，欢迎登录", Toast.LENGTH_SHORT).show()
                        ed.putString("token",data.data.userInfo.token)
                        startActivity(Intent(this,Activityhome::class.java))
                        load.hide()
                    }else {
                        Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show()
                        password.setText("")
                        load.hide()
                    }
                }
            }





        }

        register.setOnClickListener {
            startActivity(Intent(this,ActivityRegister::class.java))
        }






    }
    data class Data(
        val `data`:data2
    )
    data class data2(
        val verifySuccess:Boolean,
        val userInfo:userInfo
    )
    data class userInfo(
        val username:String,
        val token:String,
    )
}